package com.ghline.caloncabe.model;

import java.util.Random;

import com.ghline.caloncabe.GameView;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;

public class SuaBien {
	
	private static final int BMP_ROWS = 2;
    private static final int BMP_COLUMNS = 1;
    private int x = 0;
    private int y = 0;
    private int xSpeed = 0;
    private int ySpeed = 0;
    private GameView gameView;
    private Bitmap bmp;
    private int currentFrame = 0;
    private int width;
    private int height;
    private static final int MAX_SPEED = 3;
    private int addSpeed = 1;
	
	public SuaBien(GameView gameView, Bitmap bmp) {
		
		this.gameView = gameView;
        this.bmp = bmp;
        this.width = bmp.getWidth() / BMP_COLUMNS;
        this.height = bmp.getHeight() / BMP_ROWS;
        
        Random rnd = new Random();
        x = rnd.nextInt(gameView.getWidth() - this.width);
        y = rnd.nextInt(gameView.getHeight() - this.height);
        xSpeed = rnd.nextInt(MAX_SPEED * 2) - MAX_SPEED + addSpeed;
        ySpeed = rnd.nextInt(MAX_SPEED * 2) - MAX_SPEED + addSpeed;
	}
	
	private void update() {
		if (x >= gameView.getWidth() - width - xSpeed || x + xSpeed <= 0) {
            xSpeed = -xSpeed;
		}
	    x = x + xSpeed;	
	    if (y >= gameView.getHeight() - height - ySpeed || y + ySpeed <= 0) {
	        ySpeed = -ySpeed;
	    }
	    y = y + ySpeed;
	    currentFrame = ++currentFrame % BMP_ROWS;
	}

	public void draw(Canvas canvas) {	
		
		update();		
        int srcX = 0 * width;
        int srcY = currentFrame * height;
        Rect src = new Rect(srcX, srcY, srcX + width, srcY + height);
        Rect dst = new Rect(x, y, x + width, y + height);
        canvas.drawBitmap(bmp, src, dst, null);
	}
	
	public int getWidth() {
		return width;
	}
	public int getHeight() {
		return height;
	}
	public int getX() {
		return x;
	}
	public int getY() {
		return y;
	}
	public Bitmap getBitmap() {
		return bmp;
	}
	public void setBitmap(Bitmap bmp) {
		this.bmp = bmp;
	}

	public int getAddSpeed() {
		return addSpeed;
	}

	public void setAddSpeed(int addSpeed) {
		this.addSpeed = addSpeed;
	}

	public int getxSpeed() {
		return xSpeed;
	}

	public void setxSpeed(int xSpeed) {
		this.xSpeed = xSpeed;
	}

}
